<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.png">
  <link rel="mask-icon" href="/images/avatar.png" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"notes.maxwi.com","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":true,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="了解socket编程过程，根据MSDN上的Windows Sockets 2文档实现通过socket来发送和接受文件。MSDN上的具体说明：https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;ms740673%28v&#x3D;vs.85%29.aspx    对于C++实现的服务端和客户端过程大致如下：服务端：    初始化Winsock">
<meta property="og:type" content="article">
<meta property="og:title" content="使用winsock2的sockets进行文件发送">
<meta property="og:url" content="http://notes.maxwi.com/2015/11/21/winsock/index.html">
<meta property="og:site_name" content="blueyi&#39;s notes">
<meta property="og:description" content="了解socket编程过程，根据MSDN上的Windows Sockets 2文档实现通过socket来发送和接受文件。MSDN上的具体说明：https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;en-us&#x2F;library&#x2F;windows&#x2F;desktop&#x2F;ms740673%28v&#x3D;vs.85%29.aspx    对于C++实现的服务端和客户端过程大致如下：服务端：    初始化Winsock">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2015-11-21T13:10:45.000Z">
<meta property="article:modified_time" content="2015-11-21T13:10:45.000Z">
<meta property="article:author" content="blueyi">
<meta property="article:tag" content="winsock2">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://notes.maxwi.com/2015/11/21/winsock/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>使用winsock2的sockets进行文件发送 | blueyi's notes</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="blueyi's notes" type="application/atom+xml">
<link rel="alternate" href="/rss2.xml" title="blueyi's notes" type="application/rss+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">blueyi's notes</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Follow Excellence,Success will chase you!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://notes.maxwi.com/2015/11/21/winsock/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/default_avatar.jpg">
      <meta itemprop="name" content="blueyi">
      <meta itemprop="description" content="心怀善意，虛怀若谷！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="blueyi's notes">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          使用winsock2的sockets进行文件发送
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2015-11-21 21:10:45" itemprop="dateCreated datePublished" datetime="2015-11-21T21:10:45+08:00">2015-11-21</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Network/" itemprop="url" rel="index"><span itemprop="name">Network</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="Symbols count in article">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">Symbols count in article: </span>
              <span>9.5k</span>
            </span>
            <span class="post-meta-item" title="Reading time">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">Reading time &asymp;</span>
              <span>9 mins.</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>了解socket编程过程，根据MSDN上的Windows Sockets 2文档实现通过socket来发送和接受文件。MSDN上的具体说明：<a target="_blank" rel="noopener" href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms740673%28v=vs.85%29.aspx">https://msdn.microsoft.com/en-us/library/windows/desktop/ms740673%28v=vs.85%29.aspx</a>   </p>
<p>对于C++实现的服务端和客户端过程大致如下：<br>服务端：  </p>
<ul>
<li>初始化Winsock  </li>
<li>创建一个socket作为侦听服务  </li>
<li>绑定socket到系统  </li>
<li>侦听端口等待客户端连接  </li>
<li>接受客户端连接  </li>
<li>接收和发送数据  </li>
<li>断开连接  </li>
</ul>
<p>客户端：  </p>
<ul>
<li>初始化Winsock  </li>
<li>创建用于连接到服务端的socket对象  </li>
<li>连接到服务端  </li>
<li>发送数据后关闭socket连接，释放资源  </li>
<li>将该socket用于接收数据  </li>
<li>断开连接  </li>
</ul>
<span id="more"></span>
<p>具体代码如下：  </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/***************服务端*********************/</span></span><br><span class="line"><span class="meta">#<span class="keyword">undef</span> UNICODE</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> WIN32_LEAN_AND_MEAN</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;Windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;WinSock2.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;WS2tcpip.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//需要连接的库</span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> comment (lib, <span class="string">&quot;Ws2_32.lib&quot;</span>)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//定义默认接受数据缓冲区大小及侦听的端口</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEFAULT_BUFFLEN 1024</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEFAULT_PORT <span class="string">&quot;27015&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> __cdecl <span class="title">main</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//创建WSADATA对象，WSADATA中包含windows sockets 的实现信息</span></span><br><span class="line">	WSADATA wsaData;</span><br><span class="line">	<span class="comment">//接收相应函数的返回值</span></span><br><span class="line">	<span class="type">int</span> iResult;</span><br><span class="line">	<span class="comment">//声明侦听socket和连接服务端的socket</span></span><br><span class="line">	SOCKET ListenSocket = INVALID_SOCKET;</span><br><span class="line">	SOCKET ClientSocket = INVALID_SOCKET;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//声明存放ip地址信息的结构体</span></span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">addrinfo</span> *result = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">addrinfo</span> hints;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//存放接收到的内容</span></span><br><span class="line">	<span class="type">char</span> recvFileName[DEFAULT_BUFFLEN];</span><br><span class="line">	<span class="type">char</span> recvFile[DEFAULT_BUFFLEN];</span><br><span class="line">	std::string fileName;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//初始化WINSOCK</span></span><br><span class="line">	iResult = <span class="built_in">WSAStartup</span>(<span class="built_in">MAKEWORD</span>(<span class="number">2</span>, <span class="number">2</span>), &amp;wsaData);</span><br><span class="line">	<span class="keyword">if</span> (iResult != <span class="number">0</span>) &#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;WSAStartup failed with error:&quot;</span> &lt;&lt;  iResult &lt;&lt; std::endl;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//分配IP地址存放的结构体空间</span></span><br><span class="line">	<span class="built_in">ZeroMemory</span>(&amp;hints, <span class="built_in">sizeof</span>(hints));</span><br><span class="line">	hints.ai_family = AF_INET; <span class="comment">//指定使用IPV4网址地址</span></span><br><span class="line">	hints.ai_socktype = SOCK_STREAM;  <span class="comment">//指定socket流</span></span><br><span class="line">	hints.ai_protocol = IPPROTO_TCP;  <span class="comment">//指定使用TCP协议</span></span><br><span class="line">	hints.ai_flags = AI_PASSIVE;  <span class="comment">//指定一个标志位</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//获取本地IP地址信息</span></span><br><span class="line">	iResult = <span class="built_in">getaddrinfo</span>(<span class="literal">NULL</span>, DEFAULT_PORT, &amp;hints, &amp;result);</span><br><span class="line">	<span class="keyword">if</span> (iResult != <span class="number">0</span>) &#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;WSAStartup failed with error:&quot;</span> &lt;&lt;  iResult &lt;&lt; std::endl;</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建一个socket用于侦听服务器</span></span><br><span class="line">	ListenSocket = <span class="built_in">socket</span>(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);</span><br><span class="line">	<span class="keyword">if</span> (ListenSocket == INVALID_SOCKET) &#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;socket failed with error:&quot;</span> &lt;&lt;  <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">		<span class="built_in">freeaddrinfo</span>(result);</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//将侦听socket绑定到系统以用于侦听服务器</span></span><br><span class="line">	iResult = <span class="built_in">bind</span>(ListenSocket, result-&gt;ai_addr, (<span class="type">int</span>)result-&gt;ai_addrlen);</span><br><span class="line">	<span class="keyword">if</span> (iResult == SOCKET_ERROR)  &#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;bind failed with error:&quot;</span> &lt;&lt;  <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">		<span class="built_in">freeaddrinfo</span>(result);</span><br><span class="line">		<span class="built_in">closesocket</span>(ListenSocket);</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">freeaddrinfo</span>(result);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//侦听端口</span></span><br><span class="line">	iResult = <span class="built_in">listen</span>(ListenSocket, SOMAXCONN);</span><br><span class="line">	<span class="keyword">if</span> (iResult == SOCKET_ERROR) &#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;listen failed with error:&quot;</span> &lt;&lt; <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">		<span class="built_in">closesocket</span>(ListenSocket);</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span> (<span class="literal">true</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; std::endl;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;等待客户端连接...&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">		<span class="comment">//接收客户端的连接，即让客户端socket与侦听socket建立连接</span></span><br><span class="line">		ClientSocket = <span class="built_in">accept</span>(ListenSocket, <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">		<span class="keyword">if</span> (ClientSocket == INVALID_SOCKET) &#123;</span><br><span class="line">			std::cout &lt;&lt; <span class="string">&quot;accept failed with error:&quot;</span> &lt;&lt; <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">			<span class="built_in">closesocket</span>(ListenSocket);</span><br><span class="line">			<span class="built_in">WSACleanup</span>();</span><br><span class="line">			<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//接收文件名</span></span><br><span class="line">		std::ofstream out;</span><br><span class="line">		iResult = <span class="built_in">recv</span>(ClientSocket, recvFileName, DEFAULT_BUFFLEN, <span class="number">0</span>);</span><br><span class="line">		<span class="keyword">if</span> (iResult &gt; <span class="number">0</span>) &#123;</span><br><span class="line">			recvFileName[iResult] = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">			fileName = recvFileName;</span><br><span class="line">			std::cout &lt;&lt; <span class="string">&quot;接收文件名为： &quot;</span> &lt;&lt; fileName &lt;&lt; std::endl;</span><br><span class="line">			out.<span class="built_in">open</span>(fileName, std::ofstream::out);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			std::cout &lt;&lt; <span class="string">&quot;Receive file name error!&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="type">int</span> count = <span class="number">0</span>;</span><br><span class="line">		<span class="comment">//接收文件内容直到连接关闭</span></span><br><span class="line">		<span class="keyword">do</span> &#123;</span><br><span class="line">			iResult = <span class="built_in">recv</span>(ClientSocket, recvFile, DEFAULT_BUFFLEN, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">if</span> (iResult == DEFAULT_BUFFLEN) &#123;</span><br><span class="line">				<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; DEFAULT_BUFFLEN; ++i) &#123;</span><br><span class="line">					out.<span class="built_in">put</span>(recvFile[i]);</span><br><span class="line">					count++;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> <span class="keyword">if</span> (iResult &gt; <span class="number">0</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; iResult; ++i) &#123;</span><br><span class="line">					out.<span class="built_in">put</span>(recvFile[i]);</span><br><span class="line">					count++;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> <span class="keyword">if</span> (iResult == <span class="number">0</span>)</span><br><span class="line">				std::cout &lt;&lt; <span class="string">&quot;接收文件完成&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">			<span class="keyword">else</span> &#123;</span><br><span class="line">				std::cout &lt;&lt; <span class="string">&quot;recv failed with error:&quot;</span> &lt;&lt; <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">				<span class="built_in">closesocket</span>(ClientSocket);</span><br><span class="line">				<span class="built_in">WSACleanup</span>();</span><br><span class="line">				<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125; <span class="keyword">while</span> (iResult &gt; <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">		out.<span class="built_in">close</span>();</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;接收文件大小: &quot;</span> &lt;&lt; count &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//接收完成后关闭连接</span></span><br><span class="line">		iResult = <span class="built_in">shutdown</span>(ClientSocket, SD_SEND);</span><br><span class="line">		<span class="keyword">if</span> (iResult == SOCKET_ERROR) &#123;</span><br><span class="line">			std::cout &lt;&lt; <span class="string">&quot;shutdown failed with error:&quot;</span> &lt;&lt; <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">			<span class="built_in">closesocket</span>(ClientSocket);</span><br><span class="line">			<span class="built_in">WSACleanup</span>();</span><br><span class="line">			<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">		<span class="comment">//清空连接并释放资源</span></span><br><span class="line">		<span class="built_in">closesocket</span>(ClientSocket);</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/***************客户端*********************/</span></span><br><span class="line"><span class="comment">//接受两个输入参数，分别表示远程IP和传输文件名，不输入任何参数时，默认使用”127.0.0.1”和”a.txt”</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifndef</span> WIN32_LEAN_AND_MEAN</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> WIN32_LEAN_AND_MEAN</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;winsock2.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;ws2tcpip.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iphlpapi.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fstream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//需要连接的库</span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> comment(lib, <span class="string">&quot;Ws2_32.lib&quot;</span>)</span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> comment(lib, <span class="string">&quot;Mswsock.lib&quot;</span>)</span></span><br><span class="line"><span class="meta">#<span class="keyword">pragma</span> comment(lib, <span class="string">&quot;AdvApi32.lib&quot;</span>)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//远程端口</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEFAULT_PORT  <span class="string">&quot;27015&quot;</span></span></span><br><span class="line"><span class="comment">//默认缓冲区大小</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEFAULT_BUFLEN 1024</span></span><br><span class="line"><span class="function"><span class="type">int</span> __cdecl <span class="title">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> **argv)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//创建WSADATA对象，WSADATA中包含windows sockets 的实现信息</span></span><br><span class="line">	WSADATA wsaData;</span><br><span class="line">	<span class="comment">// 声明连接到服务端的socket对象</span></span><br><span class="line">	SOCKET ConnectSocket = INVALID_SOCKET;</span><br><span class="line">	<span class="comment">//声明addrinfo对象，其中包括sockaddr结构体值</span></span><br><span class="line">	<span class="comment">//result和ptr将指向相应的主机地址信息,hints存放socket属性信息</span></span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">addrinfo</span> *result = <span class="literal">NULL</span>, *ptr = <span class="literal">NULL</span>, hints;</span><br><span class="line">	<span class="comment">//向服务器发送的数据内容</span></span><br><span class="line">	<span class="type">char</span> sendBuff[DEFAULT_BUFLEN];</span><br><span class="line">	std::string fileName = <span class="string">&quot;a.txt&quot;</span>;</span><br><span class="line">	<span class="comment">//设置接收数据的缓冲区大小</span></span><br><span class="line">	<span class="type">int</span> recvbuflen = DEFAULT_BUFLEN;</span><br><span class="line">	<span class="type">char</span> recvbuf[DEFAULT_BUFLEN];</span><br><span class="line">	<span class="type">int</span> iResult;</span><br><span class="line">	std::string ip;</span><br><span class="line">	<span class="comment">//确定参数是否有效</span></span><br><span class="line">	<span class="keyword">if</span> (argc == <span class="number">2</span>) &#123;</span><br><span class="line">		ip = argv[<span class="number">1</span>];</span><br><span class="line">  std::cout &lt;&lt; <span class="string">&quot;远程服务器IP:  &quot;</span> &lt;&lt; ip &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (argc == <span class="number">3</span>) &#123;</span><br><span class="line">		ip = argv[<span class="number">1</span>];</span><br><span class="line">  std::cout &lt;&lt; <span class="string">&quot;远程服务器IP:  &quot;</span> &lt;&lt; ip &lt;&lt; std::endl;</span><br><span class="line">		fileName = argv[<span class="number">2</span>];</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> &#123;</span><br><span class="line">		ip = <span class="string">&quot;127.0.0.1&quot;</span>;</span><br><span class="line">  std::cout &lt;&lt; <span class="string">&quot;远程服务器IP:  &quot;</span> &lt;&lt; ip &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 初始化winsock </span></span><br><span class="line">	iResult = <span class="built_in">WSAStartup</span>(<span class="built_in">MAKEWORD</span>(<span class="number">2</span>, <span class="number">2</span>), &amp;wsaData);</span><br><span class="line">	<span class="keyword">if</span> (iResult != <span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;WSAStartup failed: &quot;</span> &lt;&lt; iResult &lt;&lt; std::endl;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//为hints分配空间</span></span><br><span class="line">	<span class="built_in">ZeroMemory</span>(&amp;hints, <span class="built_in">sizeof</span>(hints));</span><br><span class="line">	hints.ai_family = AF_UNSPEC;</span><br><span class="line">	hints.ai_socktype = SOCK_STREAM;</span><br><span class="line">	hints.ai_protocol = IPPROTO_TCP;</span><br><span class="line">	<span class="comment">//指定服务器地址和端口,argv[1]为IPV4或者IPV6地址</span></span><br><span class="line">	iResult = <span class="built_in">getaddrinfo</span>(ip.<span class="built_in">c_str</span>(), DEFAULT_PORT, &amp;hints, &amp;result);</span><br><span class="line">	<span class="keyword">if</span> (iResult != <span class="number">0</span>) &#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;getaddrinfo failed: &quot;</span> &lt;&lt; iResult &lt;&lt; std::endl;</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//尝试连接到服务器直到连接成功</span></span><br><span class="line">	<span class="keyword">for</span> (ptr = result; ptr != <span class="literal">NULL</span>; ptr = ptr-&gt;ai_next) &#123;</span><br><span class="line">		<span class="comment">//初始化连接到服务器的socket对象</span></span><br><span class="line">		ConnectSocket = <span class="built_in">socket</span>(ptr-&gt;ai_family, ptr-&gt;ai_socktype, ptr-&gt;ai_protocol);</span><br><span class="line">		<span class="comment">//创建失败则释放资源</span></span><br><span class="line">		<span class="keyword">if</span> (ConnectSocket == INVALID_SOCKET) &#123;</span><br><span class="line">			std::cout &lt;&lt; <span class="string">&quot;Error at socket(): &quot;</span> &lt;&lt; <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">			<span class="built_in">freeaddrinfo</span>(result);</span><br><span class="line">			<span class="built_in">WSACleanup</span>();</span><br><span class="line">			<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//连接到服务器</span></span><br><span class="line">		iResult = <span class="built_in">connect</span>(ConnectSocket, ptr-&gt;ai_addr, (<span class="type">int</span>)ptr-&gt;ai_addrlen);</span><br><span class="line">		<span class="keyword">if</span> (iResult == SOCKET_ERROR) &#123;</span><br><span class="line">			<span class="built_in">closesocket</span>(ConnectSocket);</span><br><span class="line">			ConnectSocket = INVALID_SOCKET;</span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//连接完成后释放地址信息占用的内存</span></span><br><span class="line">	<span class="built_in">freeaddrinfo</span>(result);</span><br><span class="line">	<span class="comment">//如果连接失败则返回</span></span><br><span class="line">	<span class="keyword">if</span> (ConnectSocket == INVALID_SOCKET) &#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;Unable to connect to server!&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//发送文件名</span></span><br><span class="line">	iResult = <span class="built_in">send</span>(ConnectSocket, fileName.<span class="built_in">c_str</span>(), (<span class="type">int</span>)fileName.<span class="built_in">length</span>(), <span class="number">0</span>);</span><br><span class="line">	<span class="comment">//发送失败则关闭连接</span></span><br><span class="line">	<span class="keyword">if</span> (iResult == SOCKET_ERROR) &#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;send failed: &quot;</span> &lt;&lt; <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">		<span class="built_in">closesocket</span>(ConnectSocket);</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	std::cout &lt;&lt; <span class="string">&quot;文件名所占字节数: &quot;</span> &lt;&lt; iResult &lt;&lt; std::endl;</span><br><span class="line">	std::cout &lt;&lt; <span class="string">&quot;发送文件名: &quot;</span> &lt;&lt; fileName &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//发送文件内容</span></span><br><span class="line">	<span class="function">std::ifstream <span class="title">in</span><span class="params">(fileName, std::ifstream::in)</span></span>;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span> (in.<span class="built_in">get</span>(sendBuff[i % DEFAULT_BUFLEN])) &#123;</span><br><span class="line">		i++;</span><br><span class="line">		<span class="keyword">if</span> (i % DEFAULT_BUFLEN == <span class="number">0</span>) &#123;</span><br><span class="line">			iResult = <span class="built_in">send</span>(ConnectSocket, sendBuff, <span class="built_in">sizeof</span>(sendBuff), <span class="number">0</span>);</span><br><span class="line">			<span class="comment">//发送失败则关闭连接</span></span><br><span class="line">			<span class="keyword">if</span> (iResult == SOCKET_ERROR) &#123;</span><br><span class="line">				std::cout &lt;&lt; <span class="string">&quot;send failed: &quot;</span> &lt;&lt; <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">				<span class="built_in">closesocket</span>(ConnectSocket);</span><br><span class="line">				<span class="built_in">WSACleanup</span>();</span><br><span class="line">				<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	iResult = <span class="built_in">send</span>(ConnectSocket, sendBuff, i % DEFAULT_BUFLEN, <span class="number">0</span>);</span><br><span class="line">	<span class="comment">//发送失败则关闭连接</span></span><br><span class="line">	<span class="keyword">if</span> (iResult == SOCKET_ERROR) &#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;send failed: &quot;</span> &lt;&lt; <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">		<span class="built_in">closesocket</span>(ConnectSocket);</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	std::cout &lt;&lt; <span class="string">&quot;发送文件大小: &quot;</span> &lt;&lt; i &lt;&lt; std::endl;</span><br><span class="line">	in.<span class="built_in">close</span>();</span><br><span class="line"></span><br><span class="line">	<span class="comment">//当没有数据需要发送时，首先关闭当前用于发送数据的socket,以便服务端释放资源，并将该socket用于接收数据</span></span><br><span class="line">	iResult = <span class="built_in">shutdown</span>(ConnectSocket, SD_SEND);</span><br><span class="line">	<span class="keyword">if</span> (iResult == SOCKET_ERROR) &#123;</span><br><span class="line">		std::cout &lt;&lt; <span class="string">&quot;shutdown failed: &quot;</span> &lt;&lt; <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">		<span class="built_in">closesocket</span>(ConnectSocket);</span><br><span class="line">		<span class="built_in">WSACleanup</span>();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//从服务端接收数据直到服务器关闭连接</span></span><br><span class="line">	<span class="keyword">do</span> &#123;</span><br><span class="line">		iResult = <span class="built_in">recv</span>(ConnectSocket, recvbuf, recvbuflen, <span class="number">0</span>);</span><br><span class="line">		<span class="keyword">if</span> (iResult &gt; <span class="number">0</span>) &#123;</span><br><span class="line">			std::cout &lt;&lt; <span class="string">&quot;Bytes received: &quot;</span> &lt;&lt; iResult &lt;&lt; std::endl;</span><br><span class="line">			recvbuf[iResult] = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">			std::cout &lt;&lt; <span class="string">&quot;Bytes received: &quot;</span> &lt;&lt; recvbuf &lt;&lt; std::endl;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span> (iResult == <span class="number">0</span>)</span><br><span class="line">			std::cout &lt;&lt; <span class="string">&quot;断开连接...&quot;</span> &lt;&lt; std::endl;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">			std::cout &lt;&lt; <span class="string">&quot;recv failed: &quot;</span> &lt;&lt; <span class="built_in">WSAGetLastError</span>() &lt;&lt; std::endl;</span><br><span class="line">	&#125; <span class="keyword">while</span> (iResult &gt; <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//当接收完数据之后关闭socket并释放连接资源</span></span><br><span class="line">	<span class="built_in">closesocket</span>(ConnectSocket);</span><br><span class="line">	<span class="built_in">WSACleanup</span>();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    
        

  <div class="followme">
    <p>Welcome to my other publishing channels</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/winsock2/" rel="tag"># winsock2</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2015/11/10/image-BitmapData/" rel="prev" title="图像处理相关总结及.NET的BitmapData类">
      <i class="fa fa-chevron-left"></i> 图像处理相关总结及.NET的BitmapData类
    </a></div>
      <div class="post-nav-item">
    <a href="/2015/12/02/Fortran-study-notes/" rel="next" title="Fortran 95快速学习笔记">
      Fortran 95快速学习笔记 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="blueyi"
      src="/images/default_avatar.jpg">
  <p class="site-author-name" itemprop="name">blueyi</p>
  <div class="site-description" itemprop="description">心怀善意，虛怀若谷！</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">104</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">68</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/blueyi" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;blueyi" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://maxwi.com/" title="http:&#x2F;&#x2F;maxwi.com" rel="noopener" target="_blank">Maxwi</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2014 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">blueyi</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="Symbols count total">750k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="Reading time total">11:22</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>

<script src="/js/utils.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '0f8243eb2c8b2207980f',
      clientSecret: 'd159633a33519d3b7a48b0ca729032f7d1f38a41',
      repo        : 'notes',
      owner       : 'blueyi',
      admin       : ['blueyi'],
      id          : '8468ee700a90a17036eb1d0b9279658d',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

    </div>
</body>
</html>
